package com.hq.fxh.No5_User.User.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.FavotityListGetModle;
import com.hq.fxh.Modle.SelectByCategoryIdRes;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No1_Infromation.Infor.Activity.News_DetailsActivity;
import com.hq.fxh.No1_Infromation.Infor.Adapter.Ladapter_infor;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_MyShouCang;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/21
 * @Email xlwenhao1858@sina.com
 */


public class MyShouCang extends BaseActivity implements View.OnClickListener {

    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected PullToRefreshListView listFenSi;
    private Ladapter_MyShouCang adapter;
    private int memberId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_fen_si;
    }

    @Override
    protected void initView() {
        toolbarImageBack = (ImageView) findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(MyShouCang.this);
        toolBarTvTitle = (TextView) findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) findViewById(R.id.toolbar_Layout_More);
        listFenSi = (PullToRefreshListView) findViewById(R.id.list_FenSi);


    }

    @Override
    protected void initData() {
        memberId = SharedPreferenceUtils.getMemberId(this);
        toolBarTvTitle.setText("我的收藏");
        toolBarTvMore.setVisibility(View.INVISIBLE);

        adapter = new Ladapter_MyShouCang();

        listFenSi.setAdapter(adapter);


        listFenSi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    FavotityListGetModle.DataBean itemAtPosition = (FavotityListGetModle.DataBean) adapterView.getItemAtPosition(i);
                    int favorites_type = itemAtPosition.getFavorites_type();
                    switch (favorites_type) {
                        case 0:

                            break;
                        case 1:
                            Intent intent = new Intent(MyShouCang.this, News_DetailsActivity.class);
                            intent.putExtra("type", AppConstant.Type_Favorite);
                            intent.putExtra("data", itemAtPosition);

                            intent.putExtra("url", Tool.getplateUrl(memberId, itemAtPosition.getTarget_id(), 2));
                            startActivity(intent);
                            break;
                        case 2:
                            break;
                    }

                } catch (Exception e) {
                }
            }
        });

        getFavority();
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.toolbar_Layout_back) {
            finish();
        }
    }


    void getFavority() {

        UserServiceImpl.GetFavorites(1, 20, new Callback<FavotityListGetModle>() {
            @Override
            public void onResponse(Call<FavotityListGetModle> call, Response<FavotityListGetModle> response) {
                FavotityListGetModle body = response.body();
                if (body == null) {
                    ResponseBody responseBody = response.errorBody();
                    try {
                        LogUtils.e("" + responseBody.string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    List<FavotityListGetModle.DataBean> data = body.getData();

                    adapter.setData(data);
                    adapter.notifyDataSetChanged();
                    if (data == null) {
                        LogUtils.e("" + body.getMsg());

                    } else {

                        LogUtils.e(body.toString());


                    }

                }

            }

            @Override
            public void onFailure(Call<FavotityListGetModle> call, Throwable t) {
                LogUtils.e("" + t.getMessage());

            }
        });
    }
}
